package com.insurance.modules.partner.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.insurance.common.enums.DeleteFlagEnum;
import com.insurance.common.utils.PageUtils;
import com.insurance.common.utils.R;
import com.insurance.common.utils.ShiroUtils;
import com.insurance.entity.PartnerInfoEntity;
import com.insurance.entity.PartnerRoleEntity;
import com.insurance.modules.sys.controller.AbstractController;
import com.insurance.service.PartnerInfoRoleService;
import com.insurance.service.PartnerInfoService;
import com.insurance.service.PartnerRoleService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;


/**
 * 合作方信息
 *
 * @date 2021-02-26 17:32:47
 */
@RestController
@RequestMapping("partner/partnerinfo")
public class PartnerInfoController extends AbstractController {

    @Autowired
    private PartnerInfoService partnerInfoService;

    @Autowired
    private PartnerInfoRoleService partnerInfoRoleService;

    @Autowired
    private PartnerRoleService partnerRoleService;

    /**
     * 分页列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("partner:partnerinfo:list")
    public R list(@RequestParam Map<String, Object> params, PartnerInfoEntity partnerInfoEntity){
        QueryWrapper<PartnerInfoEntity> queryWrapper = partnerInfoService.getQueryWrapper(partnerInfoEntity);
        PageUtils page = partnerInfoService.queryPage(params, queryWrapper);
        return R.ok().put("page", page);
    }

    /**
     * 列表
     * @return
     */
    @RequestMapping("/queryList")
    @RequiresPermissions("partner:partnerinfo:list")
    public R queryList(){
        //查询列表
        List<PartnerInfoEntity> partnerInfoList = partnerInfoService.queryList(new PartnerInfoEntity());
        return R.ok().put("partnerInfoList", partnerInfoList);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{partnerId}")
    @RequiresPermissions("partner:partnerinfo:info")
    public R info(@PathVariable("partnerId") String partnerId){
		PartnerInfoEntity partnerInfo = partnerInfoService.getById(partnerId);
        return R.ok().put("partnerInfo", partnerInfo);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("partner:partnerinfo:save")
    public R save(@RequestBody PartnerInfoEntity partnerInfo){
        if(!partnerInfoService.checkPartnerCode(partnerInfo.getPartnerCode())){
            return R.error("合作方编码重复");
        }
        if(!partnerInfoService.checkPartnerAppId(partnerInfo.getAppId())){
            return R.error("合作方账号重复");
        }
        partnerInfo.setPartnerId(UUID.randomUUID().toString().replaceAll("-", ""));
        partnerInfo.setCreatePersonId(String.valueOf(getUserId()));
        partnerInfo.setCreatePersonName(getUserName());
        partnerInfo.setCreateTime(new Date());
        partnerInfo.setDeleteFlag(String.valueOf(DeleteFlagEnum.UN_DELETE.getCode()));
		partnerInfoService.save(partnerInfo);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("partner:partnerinfo:update")
    public R update(@RequestBody PartnerInfoEntity partnerInfo){
        partnerInfo.setUpdateTime(new Date());
        partnerInfo.setUpdatePersonId(String.valueOf(getUserId()));
        partnerInfo.setUpdatePersonName(getUserName());
		partnerInfoService.updateById(partnerInfo);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("partner:partnerinfo:delete")
    public R delete(@RequestBody String[] partnerIds){
        PartnerInfoEntity partnerInfo = new PartnerInfoEntity();
        partnerInfo.setDeleteTime(new Date());
        partnerInfo.setDeletePersonId(getUserIdStr());
        partnerInfo.setDeletePersonName(getUserName());
        boolean flag = partnerInfoService.removeBatch(Arrays.asList(partnerIds), partnerInfo);
        return flag ? R.ok() : R.error();
    }

}
